package com.ruoyi.framework.web.service.custom;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysMiniUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.UserStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMiniUserService;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Set;

/**
 * 小程序用户登录 2022-1-18
 */
@Service
public class MiniUserDetailService implements CustomUserDetailService {

    private static final Logger log = LoggerFactory.getLogger(MiniUserDetailService.class);

    private  final String TYPE= Constants.USER_TYPE_MINI;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysMiniUserService miniUserService;

    @Autowired
    private SysPermissionService permissionService;

    @Override
    public Boolean supports(String type) {
        return TYPE.equals(type);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysMiniUser sysMiniUser =miniUserService.selectSysMiniUserByOpenId(username);
        if (StringUtils.isNull(sysMiniUser)) {
            log.info("小程序登录用户：{} 不存在.", username);
            throw new ServiceException("小程序登录用户：" + username + " 不存在");
        }
        SysUser user = new SysUser();
        /*SysUser user = userService.selectUserById(sysMiniUser.getUserId());
        //绑定用户身份（老师）
        if(StringUtils.isNull(user)) {
            SysUser userResp = userService.selectUserByPhonenumber(sysMiniUser.getTel());
            if (StringUtils.isNotNull(userResp)) {
                SysMiniUser miniUserParam = new SysMiniUser();
                miniUserParam.setMiniUserId(sysMiniUser.getMiniUserId());
                miniUserParam.setUserId(userResp.getUserId());
                miniUserParam.setRealName(userResp.getNickName());
                miniUserParam.setUnitName(userResp.getDept().getDeptName());
                miniUserParam.setSex(userResp.getSex());
                miniUserService.updateSysMiniUser(miniUserParam);
            }
        }else {
            //2022-3-11同步信息
            SysMiniUser miniUserParam = new SysMiniUser();
            miniUserParam.setMiniUserId(sysMiniUser.getMiniUserId());
            miniUserParam.setRealName(user.getNickName());
            miniUserParam.setUnitName(user.getDept().getDeptName());
            miniUserParam.setSex(user.getSex());
            miniUserService.updateSysMiniUser(miniUserParam);
        }*/
        return createLoginUser(user,sysMiniUser);
    }

    public UserDetails createLoginUser(SysUser user,SysMiniUser sysMiniUser) {

        //这个地方不区分身份 可以直接认为是小程序用户直接登录 2023-4-7
        SysUser sysUser = new SysUser();
        LoginUser  loginUser = new   LoginUser(sysUser.getUserId(),sysUser.getDeptId(),sysUser,sysMiniUser,null);
        //此处赋予权限
        /*Set<String> stringSet = new HashSet<>();
        stringSet.add("mini:list:list");*/
        /**
         * 此处权限还是跟根据user来查询。老师肯定是有权限的但是学生就没有了，可以任意操作。老师根据user获取相关信息，学生根据sysMiniUser获取相关信息
         *
         */
        /*LoginUser loginUser =null;
        if(user ==null){
            //学生登录
            SysUser sysUser = new SysUser();
            loginUser = new   LoginUser(sysUser.getUserId(),sysUser.getDeptId(),sysUser,sysMiniUser,null);
        }else{
            //老师登录
            loginUser = new LoginUser(user.getUserId(), user.getDeptId(), user,sysMiniUser,permissionService.getMenuPermission(user));
        }*/
        return loginUser;
    }
}
